package com.erp.erp_servers.Customer2.imp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.erp_dao.Customer2.ICustomer2DAO;
import com.erp.erp_entitys.Customer2.Customer2Entity;
import com.erp.erp_entitys.Customer2.Customer2Example;
import com.erp.erp_entitys.Customer2.req.supAccDeCollReq;
import com.erp.erp_entitys.Customer2.req.supAmountAndPurAmountReq;
import com.erp.erp_entitys.Customer2.res.supAccDeCollRes;
import com.erp.erp_entitys.Customer2.res.supAmountAndPurAmountRes;
import com.erp.erp_entitys.Customer2Contact.Customer2ContactEntity;
import com.erp.erp_entitys.Customer2Contact.Customer2ContactExample;
import com.erp.erp_entitys.Email.email_custor;
import com.erp.erp_servers.Customer2.ICustomer2Service;
import com.erp.erp_servers.Customer2Contact.ICustomer2ContactService;
import com.erp.erp_servers.Email.EmailService;
import com.erp.utils.StringUtil;
import com.erp.vo.SelectVo;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ImpCustomerService
 * @Description TODO
 * @Author songjizhen
 * @Date 2020-06-29 20:05
 * @Version 1.0
 */
@Service
public class ImpCustomer2Service implements ICustomer2Service {
    @Autowired
    ICustomer2DAO customer2DAO;
    @Autowired
    ICustomer2ContactService customerContactService;
    @Autowired
    EmailService emailService;

    @Override
    public long countByExample(Customer2Example example) {
        return customer2DAO.countByExample(example);
    }

    @Override
    public int deleteByExample(Customer2Example example) {
        return customer2DAO.deleteByExample(example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByExample2(Customer2Example example,List<String> list) {
        int num= customer2DAO.deleteByExample(example);
        //给邮件模块传递
        Customer2ContactExample example2=new Customer2ContactExample();
        Customer2ContactExample.Criteria criteria = example2.createCriteria();
        criteria.andCompanyIn(list);
        List<Customer2ContactEntity> customer2ContactEntities = customerContactService.selectByExample(example2);
        List<email_custor> email_custors=new ArrayList<>();
        for(Customer2ContactEntity cce:customer2ContactEntities){
            email_custor ec=new email_custor();
            ec.setId(cce.getId());;
            ec.setCode(cce.getId().toString());
            ec.setHandle(2);
            ec.setCodeClass(1);
            ec.setName(cce.getName());
            ec.setStatue(0);
            ec.setOldEmail(cce.getEmail());
            ec.setNewEmail(cce.getEmail());
            email_custors.add(ec);
        }
        if(num >0){
            num = customerContactService.deleteByExample(example2);
        }
        boolean tf;
        if(num >0){
             tf = emailService.checkUserBox(email_custors);

        }
        return num;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return customer2DAO.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Customer2Entity record) {
        return customer2DAO.insert(record);
    }

    @Override
    public int insertSelective(Customer2Entity record) {
        return customer2DAO.insertSelective(record);
    }

    @Override
    public List<Customer2Entity> selectByExample(Customer2Example example) {
        return customer2DAO.selectByExample(example);
    }

    @Override
    public Customer2Entity selectByPrimaryKey(Integer id) {
        return customer2DAO.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(Customer2Entity record, Customer2Example example) {
        return customer2DAO.updateByExampleSelective(record,example);
    }

    @Override
    public int updateByExample(Customer2Entity record, Customer2Example example) {
        return customer2DAO.updateByExample(record,example);
    }

    @Override
    public int updateByPrimaryKeySelective(Customer2Entity record) {
        return customer2DAO.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Customer2Entity record) {
        return customer2DAO.updateByPrimaryKey(record);
    }

    @Override
    public List<SelectVo> getAllList() {
        return customer2DAO.getAllList();
    }

    @Override
    public List<Customer2Entity> selectByMap(Customer2Entity record) {
        return customer2DAO.selectByMap(record);
    }

    @Override
    public List<Customer2Entity> selectByMap2(Customer2Entity record) {
        return customer2DAO.selectByMap2(record);
    }

    /**
     * 根据Id更新流程状态
     *
     * @param id
     * @param spstatus
     * @return
     */
    @Override
    public boolean updateStatus(String id, String spstatus) {
        Customer2Entity record=new Customer2Entity();
        record.setId(Integer.parseInt(id));
        record.setSpstatus(spstatus);
        int num= customer2DAO.updateByPrimaryKeySelective(record);
        return num==0?false:true;
    }
    @Override
    public Customer2Entity selectByCode(String code) {
        return customer2DAO.selectByCode(code);
    }
    /**
     * 根据供应商流程表更新供应商表审批状态
     */
    @Override
    public int updateStatusByProcessAndCustomer2() {
        int num=customer2DAO.updateStatusByProcessAndCustomer2();
        return num;
    }

    @Override
    public PageInfo<supAccDeCollRes> supAccDeColl(supAccDeCollReq req) {
        List<supAccDeCollRes> list=customer2DAO.supAccDeColl(req);
        PageInfo<supAccDeCollRes> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
    /**
     * 获取String类型不为空数据
     * @param value 目标数据
     * @return 不为空数据
     */
    private String getNotNullOfStringInfo(String value){
        if(StringUtils.isEmpty(value)){
            return "";
        }else {
            return value;
        }
    }

    /**
     * 获取BigDecimal类型不为空的数据
     * @param value 目标数据
     * @return 不为空数据
     */
    private BigDecimal getNotNullOfBigDecimalInfo(BigDecimal value){
        if(StringUtil.isNotEmpty(value)){
            return value;
        }else {
            return new BigDecimal("0");
        }
    }
    @Override
    public JSONObject supAccDeCollExport(supAccDeCollReq req) {
        List<supAccDeCollRes> list=customer2DAO.supAccDeColl(req);
        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitleSupAccDeColl();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if(CollectionUtils.isEmpty(list)){
            obj.put("data", data);
            return obj;
        }
        excelSupAccDeCollExport(data,list);
        obj.put("data", data);
        return obj;
    }
    public JSONArray getTitleSupAccDeColl(){
        JSONArray obj=new JSONArray();
        obj.add("供应商名称");
        obj.add("供应商代码");
        obj.add("供应商编码");
        obj.add("供应商类型");
        obj.add("供应商等级");
        obj.add("采购员");
        obj.add("供应商地址");
        obj.add("账期");
        obj.add("付款类型");
        return obj;
    }
    public void excelSupAccDeCollExport(JSONArray data,List<supAccDeCollRes> list ){
        for (supAccDeCollRes vo: list) {
            JSONArray infoList = new JSONArray();
            infoList.add(getNotNullOfStringInfo(vo.getSupplierName()));
            infoList.add(getNotNullOfStringInfo(vo.getSupplierNo()));
            infoList.add(getNotNullOfStringInfo(vo.getSupplierCode()));
            infoList.add(getNotNullOfStringInfo(vo.getSupplierType()));
            infoList.add(getNotNullOfStringInfo(vo.getLevelName()));
            infoList.add(getNotNullOfStringInfo(vo.getSalesmanName()));
            infoList.add(getNotNullOfStringInfo(vo.getAddress()));
            infoList.add(getNotNullOfStringInfo(vo.getPayday()));
            infoList.add(getNotNullOfStringInfo(vo.getPaymentType()));
            data.add(infoList);
        }
    }

    @Override
    public PageInfo<supAmountAndPurAmountRes> supAmountAndPurAmountPage(supAmountAndPurAmountReq req) {
        List<supAmountAndPurAmountRes> list=customer2DAO.supAmountAndPurAmountList(req);
        PageInfo<supAmountAndPurAmountRes> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public JSONObject supAmountAndPurAmountExport(supAmountAndPurAmountReq req) {
        List<supAmountAndPurAmountRes> list=customer2DAO.supAmountAndPurAmountList(req);
        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitleSupAmountAndPurAmount();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if(CollectionUtils.isEmpty(list)){
            obj.put("data", data);
            return obj;
        }
        excelSupAmountAndPurAmountExport(data,list);
        obj.put("data", data);
        return obj;
    }
    public JSONArray getTitleSupAmountAndPurAmount(){
        JSONArray obj=new JSONArray();
        obj.add("供应商名称");
        obj.add("供应商代码");
        obj.add("供应商编码");
        obj.add("供应商类型");
        obj.add("供应商等级");
        obj.add("采购员");
        obj.add("供应商地址");
        obj.add("账期");
        obj.add("采购下单总数");
        obj.add("付款类型");
        obj.add("采购总额");
        obj.add("采购总额RMB");
        obj.add("采购总额USD");
        obj.add("未出货总额");
        obj.add("未出货总额RMB");
        obj.add("未出货总额USD");
        obj.add("出货总额");
        obj.add("出货总额RMB");
        obj.add("出货总额USD");
        return obj;
    }
    public void excelSupAmountAndPurAmountExport(JSONArray data,List<supAmountAndPurAmountRes> list ){
        for (supAmountAndPurAmountRes vo: list) {
            JSONArray infoList = new JSONArray();
            infoList.add(getNotNullOfStringInfo(vo.getSupplierName()));
            infoList.add(getNotNullOfStringInfo(vo.getSupplierNo()));
            infoList.add(getNotNullOfStringInfo(vo.getSupplierCode()));
            infoList.add(getNotNullOfStringInfo(vo.getSupplierType()));
            infoList.add(getNotNullOfStringInfo(vo.getLevelName()));
            infoList.add(getNotNullOfStringInfo(vo.getSalesmanName()));
            infoList.add(getNotNullOfStringInfo(vo.getAddress()));
            infoList.add(getNotNullOfStringInfo(vo.getPayday()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getPurchaseNum()));
            infoList.add(getNotNullOfStringInfo(vo.getPaymentType()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getPurchaseAmount()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getPurchaseAmountRMB()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getPurchaseAmountUSD()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getUnCarryAmount()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getUnCarryAmountRMB()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getUnCarryAmountUSD()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getCarryAmount()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getCarryAmountRMB()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getCarryAmountUSD()));

            data.add(infoList);
        }
    }
    @Override
    public List<SelectVo> getFilterCustomer2List() {
        return customer2DAO.getFilterCustomer2List();
    }

    @Override
    public String getMaxPaymentAccountNo() {
        return customer2DAO.getMaxPaymentAccountNo();
    }
}
